package com.mohammad.fun.dtpkrisgethin;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Vibrator;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubeStandalonePlayer;

public class leg extends Activity {
	
	private static final int REQ_START_STANDALONE_PLAYER = 1;
	private static final int REQ_RESOLVE_SERVICE_MISSING = 2;

	private static final String VIDEO_ID = "3F46NZt2WEg";

	TextView timer_viewer;

	// Vibration stuff
	Vibrator mVibrator;
	long[] patternVibrate = { 0, 300, 100, 300, 100, 300 };

	CountDownTimer Timer45sec;
	CountDownTimer Timer1min;
	CountDownTimer Timer15min;
	CountDownTimer Timer2min;
	
	Intent intent;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.leg);

//		// Getting the URL to work
//		String video_path = "http://www.youtube.com/watch?v=3F46NZt2WEg";
//		Uri uri = Uri.parse(video_path);
//
//		// With this line the youtube application, if installed, will launch
//		// immediately.
//		// Without it you will be prompted with a list of the application to
//		// choose.
//		uri = Uri.parse("vnd.youtube:" + uri.getQueryParameter("v"));
//
//		intent = new Intent(Intent.ACTION_VIEW, uri);
//		intent.putExtra("force_fullscreen",true); 
		
		timer_viewer = (TextView) findViewById(R.id.timer_viewer);
		// Get instance of Vibrator from current Context
		mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

		// The current time can also be taken from "System.currentTimeMillis();"
		Timer45sec = new CountDownTimer(45000, 1000) {
			public void onTick(long millisUntilFinished) {
				timer_viewer.setText((millisUntilFinished / 1000) + " Sec");
			}

			public void onFinish() {

				timer_viewer.setText("DONE !");

				// Vibrate for the pattern only one time
				mVibrator.vibrate(patternVibrate, -1);
			}
		};
		
		
		Timer1min = new CountDownTimer(60000, 1000) {
			public void onTick(long millisUntilFinished) {
				timer_viewer.setText((millisUntilFinished / 1000) + " Sec");
			}

			public void onFinish() {

				timer_viewer.setText("DONE !");

				// Vibrate for the pattern only one time
				mVibrator.vibrate(patternVibrate, -1);
			}
		};
		
		Timer15min = new CountDownTimer(90000, 1000) {
			public void onTick(long millisUntilFinished) {
				timer_viewer.setText((millisUntilFinished / 1000) + " Sec");
			}

			public void onFinish() {

				timer_viewer.setText("DONE !");

				// Vibrate for the pattern only one time
				mVibrator.vibrate(patternVibrate, -1);
			}
		};
		
		Timer2min = new CountDownTimer(120000, 1000) {
			public void onTick(long millisUntilFinished) {
				timer_viewer.setText((millisUntilFinished / 1000) + " Sec");
			}

			public void onFinish() {

				timer_viewer.setText("DONE !");

				// Vibrate for the pattern only one time
				mVibrator.vibrate(patternVibrate, -1);
			}
		};

	}
	
	public void onPause(){
		Timer45sec.cancel();
		Timer1min.cancel();
		Timer15min.cancel();
		Timer2min.cancel();
		
		super.onPause();
	}

	public void btn_startTimer_45sec(View v) {
		Timer45sec.start();
		Timer1min.cancel();
		Timer15min.cancel();
		Timer2min.cancel();
		
	}
	
	public void btn_startTimer_1min(View v) {
		Timer45sec.cancel();
		Timer1min.start();
		Timer15min.cancel();
		Timer2min.cancel();
	}
	
	public void btn_startTimer_15min(View v) {
		Timer45sec.cancel();
		Timer1min.cancel();
		Timer15min.start();
		Timer2min.cancel();
	}
	
	public void btn_startTimer_2min(View v) {
		Timer45sec.cancel();
		Timer1min.cancel();
		Timer15min.cancel();
		Timer2min.start();
	}

	public void btn_watchVideo(View v) {
		intent = null;

		intent = YouTubeStandalonePlayer.createVideoIntent(this,
				DeveloperKey.DEVELOPER_KEY, VIDEO_ID, 0, true, true);

		if (intent != null) {
			if (canResolveIntent(intent)) {
				startActivityForResult(intent, REQ_START_STANDALONE_PLAYER);
			} else {
				// Could not resolve the intent - must need to install or update
				// the YouTube API service.
				YouTubeInitializationResult.SERVICE_MISSING.getErrorDialog(
						this, REQ_RESOLVE_SERVICE_MISSING).show();
			}
		}
		// vv.start();
		//startActivity(intent);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == REQ_START_STANDALONE_PLAYER
				&& resultCode != RESULT_OK) {
			YouTubeInitializationResult errorReason = YouTubeStandalonePlayer
					.getReturnedInitializationResult(data);
			if (errorReason.isUserRecoverableError()) {
				errorReason.getErrorDialog(this, 0).show();
			} else {
				String errorMessage = String.format(
						getString(R.string.error_player),
						errorReason.toString());
				Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
			}
		}
	}

	private boolean canResolveIntent(Intent intent) {
		List<ResolveInfo> resolveInfo = getPackageManager()
				.queryIntentActivities(intent, 0);
		return resolveInfo != null && !resolveInfo.isEmpty();
	}

}
